package com.cskaoyan.service.mallservice;

import com.cskaoyan.bean.mallbean.Region;
import com.cskaoyan.bean.mallbean.RegionExample;
import com.cskaoyan.mapper.mallmapper.RegionMapper;
import com.cskaoyan.vo.mallvo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/8 23:14
 */
@Service
public class RegionServiceImpl implements RegionService{

    @Autowired
    RegionMapper regionMapper;
    @Override
    public List<RegionVo>  queryAll() {
/**
 * [
 *     {
 *         "id": 1,
 *         "name": "北京市",
 *         "type": 1,
 *         "code": 11,
 *         "children": [{
 *             "id": 32,
 *             "name": "市辖区",
 *             "type": 2,
 *             "code": 1101,
 *             "children": [
 *                 {
 *                     "id": 376,
 *                     "name": "东城区",
 *                     "type": 3,
 *                     "code": 110101
 *                 },
 */
        //type=1 北京市、天津市、河北省等一级行政单位
        RegionExample regionExample1 = new RegionExample();
        RegionExample.Criteria criteria1 = regionExample1.createCriteria();
        criteria1.andTypeEqualTo(Byte.valueOf("1"));
        regionExample1.setOrderByClause("code");
        List<Region> regionsType1 = regionMapper.selectByExample(regionExample1);

        //ype=2 直辖市的市辖区、石家庄、涿州、邯郸等t
//        RegionExample regionExample2 = new RegionExample();
//        RegionExample.Criteria criteria2 = regionExample2.createCriteria();
//        criteria2.andTypeEqualTo(Byte.valueOf("2")).andCodeLike("%");
//        List<Region> regionsType2 = regionMapper.selectByExample(regionExample1);

        //type=3  朝阳区、津南区、某某县等区县级单位
//        RegionExample regionExample3 = new RegionExample();
//        RegionExample.Criteria criteria3 = regionExample3.createCriteria();
//        criteria3.andTypeEqualTo(Byte.valueOf("3"));
//        List<Region> regionsType3 = regionMapper.selectByExample(regionExample1);



        List<RegionVo> regionVoList = new ArrayList<>();
        for (Region region1 : regionsType1) {
            RegionVo tempRegionVo = new RegionVo();

            tempRegionVo.setId(region1.getId());
            tempRegionVo.setName(region1.getName());
            tempRegionVo.setType(region1.getType());
            tempRegionVo.setCode(region1.getCode());

            //tempRegionVo 去set childrenList
            //比如北京市、河北省的children有什么特点-->1.children们的code以父级区域的code开头
            //type=2 直辖市的市辖区、石家庄、涿州、邯郸等
            List<RegionVo.ChildrenBeanX> childrenBeanX = new ArrayList<>();


            RegionExample regionExample2 = new RegionExample();
            RegionExample.Criteria criteria2 = regionExample2.createCriteria();
//            criteria2.andTypeEqualTo(Byte.valueOf("2")).andCodeLike(region1.getCode()+"%"); //code like '11%'
            criteria2.andPidEqualTo(region1.getId());
            regionExample2.setOrderByClause("code");
            List<Region> regionsType2 = regionMapper.selectByExample(regionExample2);
            for (Region region2 : regionsType2) {
                RegionVo.ChildrenBeanX tempChildrenBeanX = new RegionVo.ChildrenBeanX();//
                tempChildrenBeanX.setId(region2.getId());
                tempChildrenBeanX.setName(region2.getName());
                tempChildrenBeanX.setType(region2.getType());
                tempChildrenBeanX.setCode(Integer.parseInt(region2.getCode()));
                //type=3  朝阳区、津南区、某某县等区县级单位
                List<RegionVo.ChildrenBeanX.ChildrenBean> childrenBeans = new ArrayList<>();


                RegionExample regionExample3 = new RegionExample();
                RegionExample.Criteria criteria3 = regionExample3.createCriteria();
//                criteria3.andTypeEqualTo(Byte.valueOf("3")).andCodeLike(region2.getCode()+"%");
                criteria3.andPidEqualTo(region2.getId());
                regionExample3.setOrderByClause("code");
                List<Region> regionsType3 = regionMapper.selectByExample(regionExample3);
                for (Region region3 : regionsType3) {
                    RegionVo.ChildrenBeanX.ChildrenBean tempChildrenBean = new RegionVo.ChildrenBeanX.ChildrenBean();
                    tempChildrenBean.setId(region3.getId());
                    tempChildrenBean.setName(region3.getName());
                    tempChildrenBean.setType(region3.getType());
                    tempChildrenBean.setCode(Integer.parseInt(region3.getCode()));

                    childrenBeans.add(tempChildrenBean);

                }
                tempChildrenBeanX.setChildren(childrenBeans);//二级区域将三级区域作为children
                childrenBeanX.add(tempChildrenBeanX);
            }

            tempRegionVo.setChildren(childrenBeanX); //一级区域将二级区域作为children
            regionVoList.add(tempRegionVo);
        }
        return regionVoList;
    }

    @Override
    public List<Region> queryAll(Integer pid) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);
        return regionMapper.selectByExample(regionExample);

    }
}
